﻿using SRB.Tg.Common;
using SRB.Tg.DrawingStandard;
using SRB.Tg.Operate;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Teigha.DatabaseServices;
using Teigha.Geometry;

namespace SRB.Tg.Model
{
    public class DrawingEngineeringQuantity
    {

        BridgeStructureType bridge_type = BridgeStructureType.AviationBridge;
        System.Data.DataTable dtProject = new System.Data.DataTable();
        System.Data.DataTable dtBridge = new System.Data.DataTable();
        System.Data.DataTable dtBridgeDrawing = new System.Data.DataTable();
        System.Data.DataTable dtProjectConfig = new System.Data.DataTable();
        string _bridgeId = null;
        DrawingConfigModel drawingConfigModel = new DrawingConfigModel();
        public DrawingEngineeringQuantity(string bridgeId, string categoryId,out System.Data.DataTable dtCategory, bool isMerge = false)
        {
            #region  参数数据初始化
            dtBridgeDrawing = new System.Data.DataTable();//图纸目录信息表
            //传入bridgeId，查询出mainId
            string sqlMainId = "select * from busi_bridge where id='" + bridgeId + "'";
            string mainId = MySqlHelper.ExecuteDataTable(sqlMainId, CommandType.Text).Rows[0]["main_id"].ToString();
            _bridgeId = mainId;
            string sqlBridge = "select * from busi_bridge_main where id='" + mainId + "'";
            dtBridge = MySqlHelper.ExecuteDataTable(sqlBridge, CommandType.Text);

            int bridge_scope = 1;//桥幅（单幅桥和双幅桥）
            if (dtBridge.Rows.Count > 0)
            {
                bridge_scope = int.Parse(dtBridge.Rows[0]["bridge_scope"].ToString());
                if (bridge_scope > 0)
                    isMerge = dtBridge.Rows[0]["drawing_level"].ToString() == "1" ? true : false; //双幅桥图纸是否合并
            }


            string sqlDrawing = "select * from busi_bridge_catalog where  (bridge_main_id='" + mainId + "' || bridge_id='"+ bridgeId + "' ) and id='" + categoryId + "'";
            dtBridgeDrawing = MySqlHelper.ExecuteDataTable(sqlDrawing, CommandType.Text);
            #endregion

            dtCategory = dtBridgeDrawing;
        }
        public void DrawingEngineeringQuantityTable(Database db, int OverAllScale, string bridgeId, string categoryId, out System.Data.DataTable dtProjectConfigTb)
        {
            try
            {
                string sqlBridgeConfig = "select * from busi_project_config where bridge_id='" + bridgeId + "'";
                dtProjectConfig = MySqlHelper.ExecuteDataTable(sqlBridgeConfig, CommandType.Text);
                PictureFrameTools.GetBridgeConfig(dtProjectConfig);

                string sqlEngineeringQuantity = "select * from ( SELECT id, materiel, materiel_code, materiel_specs,  unit,portable_belong_part,"
                                               + "sum(total_weight1)  AS total_weight1,"
                                               + "sum(total_weight2)  AS total_weight2,"
                                               + "sum(total_weight3)  AS total_weight3,"
                                               + "sum(total_weight4)  AS total_weight4,"
                                               + "sum(total_weight5)  AS total_weight5,"
                                               + "sum(total_weight6)  AS total_weight6,"
                                               + "sum(total_weight7)  AS total_weight7,"
                                               + "sum(total_weight8)  AS total_weight8,"
                                               + "sum(total_weight9)  AS total_weight9,"
                                               + "sum(total_weight)   AS total_weight"                                                                             
                         + " FROM vi_engineering_quantity where bridge_id='" + bridgeId + "' and portable_belong_part is not null   "
                         + " GROUP BY materiel_code,materiel,materiel_specs) r order by r.materiel_code,r.materiel,materiel_specs DESC";
                System.Data.DataTable dtEngineeringQuantity = MySqlHelper.ExecuteDataTable(sqlEngineeringQuantity, CommandType.Text);

                DataView dv0 = dtEngineeringQuantity.DefaultView;
                System.Data.DataTable dtDis = dv0.ToTable(true, new string[] { "portable_belong_part" });

                List<string> list = new List<string>();
                list.Add("材料");
                list.Add("材料编码");
                list.Add("规格");
                list.Add("单位");
                list.Add("钢桁架");
                list.Add("钢塔架");
                list.Add("桥台");
                list.Add("桩基");
                list.Add("桥面板");
                list.Add("护栏");
                list.Add("支座");
                list.Add("爬梯");
                list.Add("围栏");
                list.Add("合计");

                //if (dtDis.Rows.Count>0)
                //{
                //    for(int i=0;i< dtDis.Rows.Count;i++)
                //    {
                //        int type = int.Parse(dtDis.Rows[i][0].ToString());
                //        switch(type)
                //        {
                //            case 1:
                //                list.Add("钢桁架");
                //                break;
                //            case 2:
                //                list.Add("钢塔架");
                //                break;
                //            case 3:
                //                list.Add("桥台");
                //                break;
                //            case 4:
                //                list.Add("桩基");
                //                break;
                //            case 5:
                //                list.Add("桥面板");
                //                break;
                //            case 6:
                //                list.Add("护栏");
                //                break;
                //            case 7:
                //                list.Add("支座");
                //                break;
                //            case 8:
                //                list.Add("爬梯");
                //                break;
                //            case 9:
                //                list.Add("围栏");
                //                break;
                //        }
                //    }
                //}
                //list.Add("合计");


                #region 绘制图形
                drawingConfigModel = PictureFrameTools.GetDrawingConfig(dtBridgeDrawing, categoryId, dtProjectConfig);
                OverAllScale = drawingConfigModel.drawing_scale;
                Extents2d Ext, ExtNote;
                Point2d RefPt, RefPtNote;
                RefPt = Point2d.Origin;
                Ext = new Extents2d();

                List<Extents2d> exfLst = new List<Extents2d>();
                RefPtNote = Point2d.Origin.C2D(); ;
                DrawQuantityTable.DrawMaterialsTable(db, ref exfLst, RefPtNote.C3D(), dtEngineeringQuantity, list, Extensions.curFont, 1,  "主要工程量汇总表");
           


                ExtNote = new Extents2d();


                #endregion

                #region 模型空间到布局空间投影处理
                LayoutTools.CreateTopLayout(db, "图纸", exfLst, Extensions.leftWidth, Extensions.rightWidth, Extensions.bottomHeight, Extensions.topHeight, OverAllScale, 1);
                Point2d tKPoint = Point2d.Origin;

                Extents2d ex = new Extents2d();
                PictureFrameTools.AddPaperDBText(db, tKPoint, drawingConfigModel, ref ex, exfLst.Count);

                #endregion


            }
            catch (Exception ex)
            {

            }
            dtProjectConfigTb = dtProjectConfig;
        }
        public void DrawingEngineeringQuantityTableByPier(Database db, int OverAllScale, string bridgeId,string categoryId, System.Data.DataRow dr,  out System.Data.DataTable dtProjectConfigTb)
        {
            try
            {
                string sqlBridgeConfig = "select * from busi_project_config where bridge_id='" + bridgeId + "'";
                dtProjectConfig = MySqlHelper.ExecuteDataTable(sqlBridgeConfig, CommandType.Text);
                PictureFrameTools.GetBridgeConfig(dtProjectConfig);

                List<string> list = new List<string>();
                list.Add("材料");
                list.Add("材料编码");
                list.Add("规格");
                list.Add("单块重(kg)");
                list.Add("数量");
                list.Add("总重(kg)");
                list.Add("备注");


                string pierNo = dr["name"].ToString();
                string spanId = dr["id"].ToString();
                string sqlEngineeringQuantity = "select *from (SELECT id, materiel, materiel_code, materiel_specs, unit_weight, unit,length,unit_weight*volume as unit_structural_weight,  sum(number) as number, SUM(volume) volume, SUM(total_weight) AS total_weight, remark  "
            + " FROM busi_engineering_quantity where bridge_id='" + bridgeId + "'and span_id='" + spanId + "'  GROUP BY materiel,materiel_code,materiel_specs, remark,length) a order by  materiel, materiel_code,remark asc ,number,materiel_specs,length";
                System.Data.DataTable dtEngineeringQuantity = MySqlHelper.ExecuteDataTable(sqlEngineeringQuantity, CommandType.Text);


                List<string> listM = new List<string>();
                listM.Add("材料");
                listM.Add("材料编码");
                listM.Add("规格");
                listM.Add("总重(kg)");
                listM.Add("小计(吨)");
                listM.Add("合计(吨)");
                //按规格汇总
                string sqlQuantity = "SELECT id, materiel, materiel_code, materiel_specs,SUM(total_weight) AS total_weight "
          + " FROM busi_engineering_quantity where bridge_id='" + bridgeId + "'and span_id='" + spanId + "'  GROUP BY materiel_code,materiel_specs,materiel";
                System.Data.DataTable dtQuantity = MySqlHelper.ExecuteDataTable(sqlQuantity, CommandType.Text);
              
                #region 绘制图形
                drawingConfigModel = PictureFrameTools.GetDrawingConfig(dtBridgeDrawing, categoryId, dtProjectConfig);
                OverAllScale = drawingConfigModel.drawing_scale;
                Extents2d Ext, ExtNote;
                Point2d RefPt, RefPtNote;
                RefPt = Point2d.Origin;
                Ext = new Extents2d();

                List<Extents2d> exfLst = new List<Extents2d>();
                RefPtNote = Point2d.Origin.C2D(); ;
                //DrawQuantityTable.DrawSpanMaterialsTable(db, ref exfLst, RefPtNote.C3D(), dtEngineeringQuantity, list, Extensions.curFont, 1, pierNo + "墩材料汇总");
                DrawQuantityTable.DrawSpanMaterialsTableNew(db, ref exfLst, RefPtNote.C3D(), dtEngineeringQuantity, dtQuantity,list, listM, Extensions.curFont, 1, pierNo + "墩材料表", pierNo + "墩材料汇总表");


                //RefPtNote = Point2d.Origin.C2D(420 * (exfLst.Count + 1));
                //DrawQuantityTable.DrawSpanMaterialsTotalTable(db, ref Ext, RefPtNote.C3D(), dtQuantity, list, Extensions.curFont, 1, pierNo + "墩材料表");

                drawingConfigModel.name = pierNo + "墩材料汇总表";
                ExtNote = new Extents2d();


                #endregion

                #region 模型空间到布局空间投影处理
                LayoutTools.CreateLayout(db, "图纸", exfLst, Extensions.leftWidth, Extensions.rightWidth, Extensions.bottomHeight, Extensions.topHeight, OverAllScale,1);
                Point2d tKPoint = Point2d.Origin;

                Extents2d ex = new Extents2d();
                PictureFrameTools.AddPaperDBText(db, tKPoint, drawingConfigModel, ref ex, exfLst.Count);

                #endregion


            }
            catch (Exception ex)
            {

            }
            dtProjectConfigTb = dtProjectConfig;
        }

        public void DrawingUpperEngineeringQuantityTable(Database db, int OverAllScale, string bridgeId, string categoryId, out System.Data.DataTable dtProjectConfigTb)
        {
            try
            {
                string sqlBridgeConfig = "select * from busi_project_config where bridge_id='" + bridgeId + "'";
                dtProjectConfig = MySqlHelper.ExecuteDataTable(sqlBridgeConfig, CommandType.Text);
                PictureFrameTools.GetBridgeConfig(dtProjectConfig);

                string sqlEngineeringQuantity = "SELECT id, materiel, materiel_code, materiel_specs, unit_weight, unit,unit_weight*volume as unit_structural_weight,  count(*) as number, SUM(volume) volume, SUM(total_weight) AS total_weight, remark  "
                  + " FROM busi_engineering_quantity where bridge_id='" + bridgeId + "'and section_id is not null  GROUP BY materiel,materiel_code,materiel_specs,length";
                System.Data.DataTable dtEngineeringQuantity = MySqlHelper.ExecuteDataTable(sqlEngineeringQuantity, CommandType.Text);


                #region 绘制图形
                drawingConfigModel = PictureFrameTools.GetDrawingConfig(dtBridgeDrawing, categoryId, dtProjectConfig);
                OverAllScale = drawingConfigModel.drawing_scale;

                Extents2d Ext, ExtNote;
                Point2d RefPt, RefPtNote;
                RefPt = Point2d.Origin;
                Ext = new Extents2d();

                RefPtNote = Point2d.Origin;
                ExtNote = new Extents2d();


                #endregion

                #region 模型空间到布局空间投影处理
                LayoutTools.CreateLayout(db, "图纸", Ext, Extensions.leftWidth, Extensions.rightWidth, Extensions.bottomHeight, Extensions.topHeight, OverAllScale);
                Point2d tKPoint = Point2d.Origin;

                Extents2d ex = new Extents2d();
                PictureFrameTools.AddPaperDBText(db, tKPoint, 0, drawingConfigModel, ref ex, 1, 1);

                #endregion

            }
            catch (Exception ex)
            {

            }
            dtProjectConfigTb = dtProjectConfig;
        }
    }

    
}
